package com.weme.common.util

import android.util.Log
import androidx.lifecycle.ProcessLifecycleOwner
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weme.common.BaseApplication
import com.weme.common.arouter.ARouterHelper
import com.weme.common.viewmodel.ToUserInfoViewModel
import dev.kratos.api.im_server.v1.User
import dev.kratos.api.user_server.v1.CheckBlackReply
import dev.kratos.api.user_server.v1.CheckBlackRequest
import dev.kratos.api.user_server.v1.Users

object JumpUserManager {


    private val lifecycleOwner = ProcessLifecycleOwner.get()
    private val viewModel = BaseApplication.sInstance.viewModelProvider[ToUserInfoViewModel::class.java]

    private var currentUserId: Long = 0L
    private var currentUserName: String = ""
    private var currentAction: UserAction = UserAction.NONE
    private var users: Users? = null

    enum class UserAction {
        NONE, USER_INFO, CALL
    }

    fun initialize() {
     LiveEventBus.get<Any>(LiveConfig.NET_USER_INFO_AROUTER).observe(lifecycleOwner) {
            if (it is CheckBlackReply) {
                handleCheckBlackReply(it)
            }
        }
    }

    private fun handleCheckBlackReply(reply: CheckBlackReply) {
        val context = BaseApplication.sInstance.applicationContext
        when {
            reply.isBlackMe -> context.showToast(context.getString(com.example.resources.R.string.chat_block_you, currentUserName))
            reply.isBlackTo -> context.showToast(context.getString(com.example.resources.R.string.blocked_user_message, currentUserName))
            else -> when (currentAction) {
                UserAction.USER_INFO -> ARouterHelper.navigateToUserInfoActivity(UtilsApp.getTopActivity(), currentUserId)
                UserAction.CALL -> {
                    users?.let { viewModel.callsPhone(it) }
                }
                else -> Log.e("JumpUserManager", "No action to perform")
            }
        }
    }

    fun jumpUserInfoActivity(userId: Long, userName: String) {
        storeUserInfo(userId, userName, UserAction.USER_INFO)
        viewModel.isCheckBlack(CheckBlackRequest.newBuilder().setUid(userId).build())
    }


    fun jumpCallActivity(users: Users) {
        this.users = users
        storeUserInfo(users.uid, users.nickname, UserAction.CALL)
        viewModel.isCheckBlack(CheckBlackRequest.newBuilder().setUid(users.uid).build())
    }

    fun jumpCallActivity(user: User) {
        val userBuilder = Users.newBuilder()
        userBuilder.uid = user.uid
        userBuilder.nickname = user.nickname
        userBuilder.age = user.age
        userBuilder.countryCode = user.countryCode
        userBuilder.headPic = user.avatar
        this.users = userBuilder.build()
        storeUserInfo(user.uid, user.nickname, UserAction.CALL)
        viewModel.isCheckBlack(CheckBlackRequest.newBuilder().setUid(user.uid).build())
    }

    private fun storeUserInfo(userId: Long, userName: String, action: UserAction) {
        currentUserId = userId
        currentUserName = userName
        currentAction = action
    }
}
